//
// Created by tonystark on 16/3/28.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "YouDaoTabBarController.h"
#import "Define.h"
#import "MessageController.h"
#import "JobsController.h"
#import "AchieveMentController.h"
#import "MineController.h"
#import "BlurView.h"
#import "UIButton+MiddleAligning.h"

@interface YouDaoTabBarController () <UINavigationControllerDelegate> {
    UIView *_toolbarView;
    int total;
}
@end

@implementation YouDaoTabBarController {

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kClickTabbarIndex object:nil];
    _toolbarView = nil;
}

- (id)init {
    self = [super init];
    if (self) {
        [self.view addSubview:[self toolbarView]];
        self.viewControllers = [self createViews];
        self.selectedIndex = 0;
        //监听是否强制点击Tabbar
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClickTabbarAtIndex:) name:kClickTabbarIndex object:nil];
    }
    return self;
}

- (UIView *)topView {
    return nil;
}

#pragma mark

- (NSArray *)createViews {
    MessageController *messageController = [MessageController new];
    JobsController *jobsController = [JobsController new];
    AchieveMentController *achieveMentController = [AchieveMentController new];
    MineController *mineController = [MineController new];
    return @[messageController, jobsController, achieveMentController, mineController];
}


- (void)didClickTabbarAtIndex:(NSNotification *)notification {
    int index = 0;
    NSDictionary *info = notification.userInfo;
    if (info) {
        index = [info[@"index"] intValue];
    }
    index = MIN(self.viewControllers.count - 1, index);
    [self setSelectedIndex:index];
}

- (void)removeViewController:(UIViewController *)controller {
    [controller willMoveToParentViewController:nil];
    [controller.view removeFromSuperview];
    [controller removeFromParentViewController];
}

- (void)addViewController {
    UIViewController *controller = _viewControllers[_selectedIndex];
    [self addChildViewController:controller];
    [self.view insertSubview:controller.view atIndex:0];
//    controller.view.frameY = 0;
    [controller didMoveToParentViewController:self];
}

#pragma mark - property

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    NSInteger oldIndex = _selectedIndex;
    _selectedIndex = selectedIndex;
    // 修改按钮状态
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *btn = (UIButton *) [_toolbarView viewWithTag:i + 100];
        btn.selected = i == _selectedIndex;
    }
    // 切换视图
    [self removeViewController:_viewControllers[oldIndex]];
    [self addViewController];
}

- (UIViewController *)selectedViewController {
    return _viewControllers[_selectedIndex];
}

#pragma mark - view

- (UIView *)toolbarView {
    if (!_toolbarView) {
        BlurView *barView = [[BlurView alloc] initWithFrame:CGRectMake(0, deviceHeight - tabBarHeight, deviceWidth, tabBarHeight)];
        NSArray *_iconNames = @[@"message-d", @"jobs-d", @"achivement-d", @"me-d"];
        NSArray *_iconNames2 = @[@"message-c", @"jobs-c", @"achivement-c", @"me-c"];
        NSArray *_iconNameTexts = @[@"消息", @"工作", @"绩效", @"我"];
        total = _iconNames.count;
        for (NSUInteger i = 0; i < total; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(deviceWidth / total * i, 4, deviceWidth / total, tabBarHeight - 4);
            [btn setImage:[UIImage imageNamed:_iconNames[i]] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:_iconNames2[i]] forState:UIControlStateSelected];
            [btn setTitle:_iconNameTexts[i] forState:UIControlStateNormal];
            [btn setTitleColor:UIColorFromRGB(0x929292) forState:UIControlStateNormal];
            [btn setTitleColor:UIColorFromRGB(0x2D9AEC) forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(toolbarBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont systemFontOfSize:10];
            [btn middleAlignButtonWithSpacing:4];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.tag = i + 100;
            [barView addSubview:btn];
        }
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, deviceWidth, gLineWidthOrHeight)];
        lineView.backgroundColor = UIColorFromRGB(0xB2B2B2);
        [barView addSubview:lineView];
        lineView.tag = 9999;
        _toolbarView = barView;
    }

    return _toolbarView;
}

#pragma mark - action

- (void)toolbarBtnPressed:(UIButton *)sender {
    NSInteger index = sender.tag - 100;
    if (index == 0) {
        self.selectedIndex = 0;
        // 通知回调
        if (_delegate && [_delegate respondsToSelector:@selector(youDaoSTabBarViewCOntroller:didSelectViewController:)]) {
            [_delegate youDaoSTabBarViewCOntroller:self didSelectViewController:_viewControllers[_selectedIndex]];
        }
    } else {
        if (_selectedIndex != index) {
            self.selectedIndex = index;
        }
    }
}
@end